<?php
namespace Home\Controller;
use Think\Controller;
use Common\Model\CurrencyModel;
use Common\Model\UserMoneyModel;
use Common\Model\NoticeModel;
use Common\Model\UserBankModel;
use Common\Model\LeavelModel;
use Common\Model\UserModel;
class UserController extends HomeController {
    public function __construct()
    {
        parent::__construct();
    }
    #登录首页
    public function index()
    {
        $currency=CurrencyModel::newSelf()->getInfo();
        $user_money=UserMoneyModel::newSelf()->getUserMoneyInfo($this->home_user_info['id']);
        
        
        $this->assign('currency',$currency);
        $this->assign('user_money',$user_money);

        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);
        $this->assign('notice',NoticeModel::newSelf()->pagingList());
        $this->display();
    }
    #修改信息页
    public function information()
    {
        $country=$this->getCountry();
        $this->assign('country',$country);
        

        $bank_type=$this->getBankType();
        $this->assign('bank_type',$bank_type);
        
        $user_bank=UserBankModel::newSelf()->findOneByUserId($this->home_user_info['id']);
        $this->assign('user_bank',$user_bank);
        


        $leavel_type=LeavelModel::newSelf()->getInfo();
        $this->assign('leavel_type',$leavel_type);
        
        $this->display();
    }
    #修改信息提交
    public function informationSub()
    {
        if(empty($this->home_user_info['realname'])){
        $data['realname']=I('realname');
        }
        if(empty($this->home_user_info['idcard'])){
        $data['idcard']=I('idcard');
        }
        if(empty($this->home_user_info['phone'])){
        $data['phone']=I('phone');
        }
        if(empty($this->home_user_info['wechat'])){
          $data['wechat']=I('wechat');
        }
        if(empty($this->home_user_info['qq'])){
        $data['qq']=I('qq');
        }
        if(empty($this->home_user_info['email'])){
        $data['email']=I('email');
        }
        if(empty($this->home_user_info['address'])){
        $data['address']=I('address');
        }
        if(empty($this->home_user_info['city'])){
        $data['city']=I('city');
        }
        if(empty($this->home_user_info['state'])){
        $data['state']=I('state');
        }
        if(empty($this->home_user_info['country'])){
        $data['country']=I('country');
        }
        if(empty($this->home_user_info['zhifubao'])){
          $data['zhifubao']=I('zhifubao');
        }
        
        UserModel::newSelf()->where(['id'=>$this->home_user_info['id']])->save($data);
        $user_bank=UserBankModel::newSelf()->findOneByUserId($this->home_user_info['id']);

        if(empty($user_bank['type'])){
          $bank_data['type']=I('bank_type');
        }
        if(empty($user_bank['bank_card'])){
            $bank_data['card']=I('bank_card');
        }
        if(empty($user_bank['bank_realname'])){
          $bank_data['realname']=I('bank_realname');
        }
        if(empty($user_bank['bank_address'])){
          $bank_data['address']=I('bank_address');
        }
        UserBankModel::newSelf()->where(['user_id'=>$this->home_user_info['id']])->save($bank_data);
        
        $this->returnResult('修改成功',true);
    }
    #修改密码页
    public function changePassword()
    {
        $this->display();
    }
    #修改密码提交
    public function changePasswordSub()
    {
        $password1=I('password1');
        $password2=I('password2');
        $password=I('password');
        
        $this->notEmptyValidate($password1,'新密码不能为空');
        $this->notEmptyValidate($password2,'确认密码不能为空');
        
        $this->eqValidate($password1, $password2,'新密码与确认密码不同');
        
        $model=UserModel::newSelf();
        
        $this->trueValidate($model->checkUserPassword($password, $this->home_user_info['password']),'原密码不正确');
        
        $res=$model->setUserPassword($password1, $this->home_user_info['id']);
        
        $this->resReturn($res,'修改成功', '修改失败');
        
    }
    #修改二级密码页
    public function changeTwoPassword()
    {
        $this->display();
    }
    #修改二级密码提交
    public function changeTwoPasswordSub()
    {
        $password1=I('password1');
        $password2=I('password2');
        $password=I('password');
        
        $this->notEmptyValidate($password1,'新密码不能为空');
        $this->notEmptyValidate($password2,'确认密码不能为空');
        
        $this->eqValidate($password1, $password2,'新密码与确认密码不同');
        
        $model=UserModel::newSelf();
        
        $this->trueValidate($model->checkUserTwoPassword($password, $this->home_user_info['twopassword']),'原密码不正确');
        
        $res=$model->setUserTwoPassword($password1,$this->home_user_info['id']);
        
        $this->resReturn($res,'修改成功', '修改失败');
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}